package cn.edu.zjicm.wordsnet_d.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.activity.base.WordBaseActivity;
import cn.edu.zjicm.wordsnet_d.download.UpdateManager;
import cn.edu.zjicm.wordsnet_d.view.PageControl;

import com.umeng.analytics.MobclickAgent;

public class ShowListenApp extends WordBaseActivity {
	private ViewPager mViewPager;
	private PageControl dot;
	private int[] guideDrawables;// 一个包含资源图片DrawalbeId的数组,Guide界面会依次显示资源,通过setGuideDrawables设置

	public static void startActivity(Context context) {
		Intent i = new Intent(context, ShowListenApp.class);
		context.startActivity(i);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitleBarStyle(TITLE_BAR_STYLE_BACK_BUTTON);
		setTitleText("知米听力");
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_show_listen_app);
		findViews();
		init();
	}

	private void findViews() {
		mViewPager = (ViewPager) findViewById(R.id.flipper);
		dot = (PageControl) findViewById(R.id.dot);
		findViewById(R.id.start_button).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startDownloadListenApp();
			}
		});
	}

	private void init() {
		guideDrawables = setGuideDrawables();
		mViewPager.setAdapter(new PagerAdapter() {
			@Override
			public boolean isViewFromObject(View arg0, Object arg1) {
				return arg0 == arg1;
			}

			@Override
			public int getCount() {
				return guideDrawables.length;
			}

			@Override
			public void destroyItem(ViewGroup view, int position, Object object) {
				// view.removeView();
			}

			@Override
			public Object instantiateItem(ViewGroup view, int position) {
				View v = LayoutInflater.from(ShowListenApp.this).inflate(R.layout.view_show_listen_app, null);
				ImageView image = (ImageView) v.findViewById(R.id.image);
				image.setImageResource(guideDrawables[position]);
				view.addView(v);
				return v;
			}
		});
		dot.setPointCount(guideDrawables.length);
		dot.setOnPageControlListener(mViewPager, dot);
	}

	private int[] setGuideDrawables() {
		int[] guidePics = new int[5];
		guidePics[0] = R.drawable.listen_introduction1;
		guidePics[1] = R.drawable.listen_introduction2;
		guidePics[2] = R.drawable.listen_introduction3;
		guidePics[3] = R.drawable.listen_introduction4;
		guidePics[4] = R.drawable.listen_introduction5;
		return guidePics;
	}

	private void startDownloadListenApp() {
		UpdateManager.downloadZhimiListenExludingMarket(this);
		// finish();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPause(this);
	}
}
